/*
 ============================================================================
 Name		: ActivityManager.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CActivityManager implementation
 ============================================================================
 */

#include "ActivityManager.h"

CActivityManager* CActivityManager::NewL(
		MActivityManagerObserver* aObserver, TInt aTimeout)
	{
	CActivityManager* self = new (ELeave) CActivityManager(aObserver, aTimeout);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}

TBool CActivityManager::Start()
	{
	TInt inactivity = User::InactivityTime().Int();
	TBool userIsActive = (inactivity < iTimeout);
	if (!IsActive())
		{
		if (userIsActive)
			{
			iWatch = EWaitingForInactivity;
			iTimer.Inactivity(iStatus, iTimeout);
			SetActive();
			}
		else
			{
			iWatch = EWaitingForActivity;
			iTimer.Inactivity(iStatus, 0); // See explanation for the RunL method
			SetActive();
			}
		}

	return userIsActive;
	}

CActivityManager::CActivityManager(MActivityManagerObserver* aObserver,
		TInt aTimeout) :
	CActive(CActive::EPriorityHigh), iObserver(aObserver), iTimeout(aTimeout)
	{
	CActiveScheduler::Add(this);
	}

CActivityManager::~CActivityManager()
	{
	Cancel();
	iTimer.Close();
	}

void CActivityManager::ConstructL()
	{
	iTimer.CreateLocal();
	}

void CActivityManager::SetTimeout(TInt aTimeout)
	{
	iTimeout = aTimeout;
	Reset();
	}

void CActivityManager::Reset()
	{
	Cancel();
	Start();
	}

void CActivityManager::DoCancel()
	{
	iTimer.Cancel();
	iWatch = EWaitingForNone;
	}

void CActivityManager::RunL()
	{
	if (iStatus == KErrNone)
		{
		if (iWatch == EWaitingForInactivity)
			{
			TInt inactivity = User::InactivityTime().Int();

			if (inactivity >= iTimeout)
				{
				if (iObserver)
					{
					iObserver->InactivityDetected();
					}

				if (!IsActive()) //observer might have called a Reset()
					{
					iTimer.Inactivity(iStatus, 0);
					iWatch = EWaitingForActivity;
					}
				}
			else
				{
				iTimer.Inactivity(iStatus, iTimeout);
				}
			}
		else if (iWatch == EWaitingForActivity)
			{
			if (iObserver)
				{
				iObserver->ActivityDetected();
				}

			if (!IsActive()) //observer might have called a Reset()
				{
				iTimer.Inactivity(iStatus, iTimeout);
				iWatch = EWaitingForInactivity;
				}
			}

		if (!IsActive()) //observer might have called a Reset()
			{
			SetActive();
			}
		}
	else
		{
		iWatch = EWaitingForNone;
		}
	}
